package com.ruoyi.drug.service.impl;

import com.ruoyi.drug.domain.DrugsDurg;
import com.ruoyi.drug.domain.DrugsOrderDetail;
import com.ruoyi.drug.mapper.DrugsDurgMapper;
import com.ruoyi.drug.mapper.DrugsOrderDetailMapper;
import com.ruoyi.drug.service.IDrugsOrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 订单明细Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-14
 */
@Service
public class DrugsOrderDetailServiceImpl implements IDrugsOrderDetailService
{
    @Autowired
    private DrugsOrderDetailMapper drugsOrderDetailMapper;

    @Autowired
    private DrugsDurgMapper drugsDurgMapper;

    /**
     * 查询订单明细
     *
     * @param detailId 订单明细主键
     * @return 订单明细
     */
    @Override
    public DrugsOrderDetail selectDrugsOrderDetailByDetailId(Long detailId)
    {
        return drugsOrderDetailMapper.selectDrugsOrderDetailByDetailId(detailId);
    }

    /**
     * 查询订单明细列表
     *
     * @param drugsOrderDetail 订单明细
     * @return 订单明细
     */
    @Override
    public List<DrugsOrderDetail> selectDrugsOrderDetailList(DrugsOrderDetail drugsOrderDetail)
    {
        return drugsOrderDetailMapper.selectDrugsOrderDetailList(drugsOrderDetail);
    }

    /**
     * 新增订单明细
     *
     * @param drugsOrderDetail 订单明细
     * @return 结果
     */
    @Override
    public int insertDrugsOrderDetail(DrugsOrderDetail drugsOrderDetail)
    {
        return drugsOrderDetailMapper.insertDrugsOrderDetail(drugsOrderDetail);
    }

    @Override
    public void insertDrugsOrderDetails(Long[] drugIds,Long orderId) {

        for (Long id : drugIds) {
            DrugsDurg drugsDurg = drugsDurgMapper.selectDrugsDurgByDurgId(id);
            DrugsOrderDetail drugsOrderDetail = new DrugsOrderDetail();
            drugsOrderDetail.setDetailProduceId(drugsDurg.getDurgProducer()+"");
            drugsOrderDetail.setDetailDrugId(drugsDurg.getDurgId());
            drugsOrderDetail.setDetailDrugName(drugsDurg.getDurgName());
            drugsOrderDetail.setOrderId(orderId);
            drugsOrderDetail.setDetailDrugNum(1L);
            drugsOrderDetail.setDetailDrugPrice(drugsDurg.getDurgOtcPrice());
            drugsOrderDetail.setDetailBathNum(1L);
            drugsOrderDetail.setDetailTotalPrice(drugsDurg.getDurgOtcPrice());
            drugsOrderDetail.setDetailNorms(drugsDurg.getDurgUnit());
            drugsOrderDetailMapper.insertDrugsOrderDetail(drugsOrderDetail);
        }
    }

    /**
     * 修改订单明细
     *
     * @param drugsOrderDetail 订单明细
     * @return 结果
     */
    @Override
    public int updateDrugsOrderDetail(DrugsOrderDetail drugsOrderDetail)
    {
        return drugsOrderDetailMapper.updateDrugsOrderDetail(drugsOrderDetail);
    }

    /**
     * 批量删除订单明细
     *
     * @param detailIds 需要删除的订单明细主键
     * @return 结果
     */
    @Override
    public int deleteDrugsOrderDetailByDetailIds(Long[] detailIds)
    {
        return drugsOrderDetailMapper.deleteDrugsOrderDetailByDetailIds(detailIds);
    }

    /**
     * 删除订单明细信息
     *
     * @param detailId 订单明细主键
     * @return 结果
     */
    @Override
    public int deleteDrugsOrderDetailByDetailId(Long detailId)
    {
        return drugsOrderDetailMapper.deleteDrugsOrderDetailByDetailId(detailId);
    }
}
